/*---------------------------------------------------------------------------*/
/*!				\file				Settings.c
				\date				march 2010
				\author				TNick

				\brief				Loading and saving settings
    
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */
	#include	"Config.h"
	#include	<windows.h>
	#include	<Commctrl.h>
	
	#include	"main.h"
	
/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
void	GetSettings			(SETT* pStg)
{
	HKEY			hkey;  // Handle to registry key
	unsigned long	datalen;  // data field length(in), data returned length(out) 
	unsigned long	datatype; // #defined in winnt.h (predefined types 0-11)
	char			data[256];
	unsigned long	i;
	unsigned long	j;
	
	if (RegOpenKeyEx(HKEY_CURRENT_USER, 
			AppRegKey,
			0,
			KEY_QUERY_VALUE,
			&hkey) != ERROR_SUCCESS)
	{
		DEBUGGLE("Failed to open application key!");
		return;
	}
	
	
	datatype = REG_BINARY;
	datalen = 255;
	if (RegQueryValueEx(hkey,TEXT("stgs"),NULL,
		&datatype,data,	&datalen) != ERROR_SUCCESS)
	{
		DEBUGGLE("Failed to read main settings key!")
		RegCloseKey(hkey);
		return;
	}
	else
	{
		pStg->ShowSrg.Letter = data[0];
		pStg->ShowSrg.Ctrl = data[1];
		pStg->ShowSrg.Alt = data[2];
		pStg->ShowSrg.Shift = data[3];
		
		pStg->HideSrg.Letter = data[4];
		pStg->HideSrg.Ctrl = data[5];
		pStg->HideSrg.Alt = data[6];
		pStg->HideSrg.Shift = data[7];
		
		pStg->ShowGest[0] = data[8];
		pStg->ShowGest[1] = data[9];
		pStg->ShowGest[2] = data[10];
		pStg->ShowGest[3] = data[11];
		
		pStg->HideGest[0] = data[12];
		pStg->HideGest[1] = data[13];
		pStg->HideGest[2] = data[14];
		pStg->HideGest[3] = data[15];
		
		pStg->Flags = *((APPFL*)&data[16]) & APPFL_MASK;
		pStg->EpocsToWait = *((unsigned*)&data[24]) & 0xFFFF;
		pStg->Width_Left = *((LONG*)&data[32]) & 0xFFFFFFFF;
		
		pStg->rMW.left = *((LONG*)&data[40]) & 0xFFFFFFFF;
		pStg->rMW.top = *((LONG*)&data[48]) & 0xFFFFFFFF;
		pStg->rMW.right = *((LONG*)&data[56]) & 0xFFFFFFFF;
		pStg->rMW.bottom = *((LONG*)&data[64]) & 0xFFFFFFFF;
		
		j = 72;
		for (i = 0; i < LISTCOLS_CNT; i++)
		{
		
			pStg->LsL_ColW[i] = *((LONG*)&data[j+0]) & 0xFFFFFFFF;
			pStg->LsR_ColW[i] = *((LONG*)&data[j+8]) & 0xFFFFFFFF;
			j += 16;
		}
	}
	
	RegCloseKey(hkey);
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void	SaveSettings		(SETT* pStg)
{
	HKEY			hkey;  // Handle to registry key
	HKEY			hkeyT;
	unsigned long	datatype; // #defined in winnt.h (predefined types 0-11)
	char			data[256];
	unsigned long	i;
	unsigned long	j;
	
	memset(&data,0,256);
	if (RegOpenKeyEx(HKEY_CURRENT_USER, 
			AppRegKey,
			0,
			KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
			&hkey) != ERROR_SUCCESS)
	{
		if(RegOpenKeyEx(HKEY_CURRENT_USER, 
			AppRegKeyHost,
			0,
			KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
			&hkeyT) != ERROR_SUCCESS)
		{
			DEBUGGLE("Failed to open application key!")
			return;
		}
		if(RegCreateKey(hkeyT,AppRegKeyName,&hkey) != ERROR_SUCCESS)
		{
			DEBUGGLE("Failed to create application key!")
			RegCloseKey(hkeyT);
			return;
		}
		RegCloseKey(hkeyT);
	}
	

	data[0] = pStg->ShowSrg.Letter ;
	data[1] = pStg->ShowSrg.Ctrl ;
	data[2] = pStg->ShowSrg.Alt ;
	data[3] = pStg->ShowSrg.Shift ;

	data[4] = pStg->HideSrg.Letter ;
	data[5] = pStg->HideSrg.Ctrl ;
	data[6] = pStg->HideSrg.Alt ;
	data[7] = pStg->HideSrg.Shift ;

	data[8] = pStg->ShowGest[0] ;
	data[9] = pStg->ShowGest[1] ;
	data[10] = pStg->ShowGest[2] ;
	data[11] = pStg->ShowGest[3] ;

	data[12] = pStg->HideGest[0] ;
	data[13] = pStg->HideGest[1] ;
	data[14] = pStg->HideGest[2] ;
	data[15] = pStg->HideGest[3] ;
	*((APPFL*)&data[16]) = pStg->Flags;
	*((unsigned*)&data[24]) = pStg->EpocsToWait;
	*((LONG*)&data[32]) = pStg->Width_Left;
	GetWindowRect(hwMain,&pStg->rMW);
	
	pStg->rMW.right -= pStg->rMW.left;
	pStg->rMW.bottom -= pStg->rMW.top;
	
	*((LONG*)&data[40]) = pStg->rMW.left;
	*((LONG*)&data[48]) = pStg->rMW.top;
	*((LONG*)&data[56]) = pStg->rMW.right;
	*((LONG*)&data[64]) = pStg->rMW.bottom;
	
	j = 72;
	for (i = 0; i < LISTCOLS_CNT; i++)
	{
		pStg->LsL_ColW[i] = ListView_GetColumnWidth(hW_Ls_L,i);
		pStg->LsR_ColW[i] = ListView_GetColumnWidth(hW_Ls_R,i);

		*((LONG*)&data[j+0]) = pStg->LsL_ColW[i];
		*((LONG*)&data[j+8]) = pStg->LsR_ColW[i];
		j += 16;
	}
	
	datatype = REG_BINARY;
	if (RegSetValueEx(hkey,TEXT("stgs"), 0, REG_BINARY, data, j) != ERROR_SUCCESS)
	{
		DEBUGGLE("Failed to save main settings key!")
	}
	RegCloseKey(hkey);
	return;
		
}
/* ========================================================================= */


/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
